package com.dyh.quickdemozwei.dyhTp;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author: yhdong
 * @Date: 2021/10/1 17:08
 */
public class DyhIsolateThreadPool {

    int capacity;

    static  ConcurrentHashMap<Integer,DyhThreadPool> poolMap;

    public DyhIsolateThreadPool(int capacity) {
        this.capacity = capacity;
        poolMap =  new ConcurrentHashMap<>(capacity);
        for (int i=0;i<capacity;i++){

            DyhQueue<Runnable> dyhQueue = new DyhQueue<>(1);//cant be 0
            DyhThreadPool dyhThreadPool = new DyhThreadPool(
                    1,4,120, TimeUnit.SECONDS,
                    dyhQueue,
                    new ThreadFactoryBuilder().setNameFormat("room-"+i+"DyhIsolateThreadPool-%d").build(),
                    //   new ThreadPoolExecutor.CallerRunsPolicy()
                    new ThreadPoolExecutor.AbortPolicy()
            );

            dyhQueue.setExecutor(dyhThreadPool);//bind

            poolMap.put(i,dyhThreadPool);
        }
    }

    public DyhThreadPool getPool(Integer idx){
        return poolMap.get(idx);
    }


}
